package com.jn.zhinongbao.adapter.adapter;

import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.supportingagriculturaltreasures.R;
import com.example.supportingagriculturaltreasures.bean.FarmerCircle;

import java.io.File;
import java.util.List;

public class FarmerCircleAdapter extends RecyclerView.Adapter<FarmerCircleAdapter.FarmerCircleViewHolder> {

    private List<FarmerCircle> circleList;
    private OnFarmerCircleActionListener listener;

    public interface OnFarmerCircleActionListener {
        void onLikeClick(FarmerCircle circle);
        void onCommentClick(FarmerCircle circle);
        void onShareClick(FarmerCircle circle);
        void onItemClick(FarmerCircle circle);
    }

    public FarmerCircleAdapter(List<FarmerCircle> circleList, OnFarmerCircleActionListener listener) {
        this.circleList = circleList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public FarmerCircleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_farmer_circle, parent, false);
        return new FarmerCircleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FarmerCircleViewHolder holder, int position) {
        FarmerCircle circle = circleList.get(position);
        holder.bind(circle);
    }

    @Override
    public int getItemCount() {
        return circleList.size();
    }

    class FarmerCircleViewHolder extends RecyclerView.ViewHolder {
        private TextView tvAuthorName, tvContent, tvLocation, tvTime;
        private TextView tvLikeCount, tvCommentCount;
        private ImageView ivImage, ivLike, ivComment, ivShare;

        public FarmerCircleViewHolder(@NonNull View itemView) {
            super(itemView);
            tvAuthorName = itemView.findViewById(R.id.tv_author_name);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvLocation = itemView.findViewById(R.id.tv_location);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvLikeCount = itemView.findViewById(R.id.tv_like_count);
            tvCommentCount = itemView.findViewById(R.id.tv_comment_count);
            ivImage = itemView.findViewById(R.id.iv_image);
            ivLike = itemView.findViewById(R.id.iv_like);
            ivComment = itemView.findViewById(R.id.iv_comment);
            ivShare = itemView.findViewById(R.id.iv_share);
        }

        public void bind(FarmerCircle circle) {
            tvAuthorName.setText(circle.getAuthorName());
            tvContent.setText(circle.getContent());
            tvLocation.setText(circle.getLocation());
            tvTime.setText(circle.getCreateTime());
            tvLikeCount.setText(String.valueOf(circle.getLikeCount()));
            tvCommentCount.setText(String.valueOf(circle.getCommentCount()));

            // 设置点赞状态
            if (circle.isLiked()) {
                ivLike.setImageResource(android.R.drawable.btn_star_big_on);
            } else {
                ivLike.setImageResource(android.R.drawable.btn_star_big_off);
            }

            // 加载图片
            if (circle.getImageUrl() != null && !circle.getImageUrl().isEmpty()) {
                ivImage.setVisibility(View.VISIBLE);
                loadImage(circle.getImageUrl());
            } else {
                ivImage.setVisibility(View.GONE);
            }

            // 设置点击事件
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemClick(circle);
                }
            });

            ivLike.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onLikeClick(circle);
                }
            });

            ivComment.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onCommentClick(circle);
                }
            });

            ivShare.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onShareClick(circle);
                }
            });
        }

        private void loadImage(String imageUrl) {
            if (imageUrl != null && !imageUrl.isEmpty()) {
                // 首先尝试作为文件路径加载
                File imageFile = new File(imageUrl);
                if (imageFile.exists()) {
                    Glide.with(itemView.getContext())
                            .load(Uri.fromFile(imageFile))
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .listener(new RequestListener<Drawable>() {
                                @Override
                                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                    // 加载失败时显示默认图片
                                    ivImage.setImageResource(android.R.drawable.ic_menu_gallery);
                                    return false;
                                }

                                @Override
                                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                    return false;
                                }
                            })
                            .into(ivImage);
                } else {
                    // 如果不是文件路径，尝试作为URI加载
                    Glide.with(itemView.getContext())
                            .load(imageUrl)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .listener(new RequestListener<Drawable>() {
                                @Override
                                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                    ivImage.setImageResource(android.R.drawable.ic_menu_gallery);
                                    return false;
                                }

                                @Override
                                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                    return false;
                                }
                            })
                            .into(ivImage);
                }
            } else {
                ivImage.setImageResource(android.R.drawable.ic_menu_gallery);
            }
        }
    }
} 